package filesOperations;
import filesOperation.FilesWork;

import org.testng.annotations.Parameters;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.assertTrue;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


public class HomeTest2 {


    @BeforeClass
    @Parameters({"string1","string2","string3","filename"})
    public void createFileWithStrings(String string1, String string2, String string3, String filename ) {

        FilesWork.createFile(string1, string2, string3, filename);
        System.out.println("1. File for testing created");

    }


    @Test(alwaysRun = true)
    @Parameters("filename")
    public static void testFile(String filename ) throws IOException {

        BufferedReader file = null;
        try {
            file = new BufferedReader(new FileReader(filename));
            String line;
            // assume that all numbers we expect in a file are >= 0
            int number = 0;
            System.out.println("2. Numbers of strings: ");
            while ((line = file.readLine()) != null) {

                String[] words = line.split("[ ]");
                int newNumber = Integer.parseInt(words[1]);
                System.out.println(newNumber);

                assertTrue(newNumber >= number);
                number = newNumber;

            }

            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    @AfterClass(alwaysRun = true)
    @Parameters("filename")
    public void removeFile(String filename) {
        File file = new File(filename);
        file.delete();
        System.out.println("3. File for testing deleted");
    }


}
